package vn.sunnet.game.SinhTon.scroll;

import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Array;

public class ButtonLevel extends Group {

	private int id;
	private Image img;

	private onClickListener listener;

	public interface onClickListener {
		public void onUp(int id);
	}

	public ButtonLevel(int id, Array<AtlasRegion> tr, onClickListener listener) {
		super();

		this.id = id;
		this.listener = listener;

		img = new Image(tr.get(id - 1));
		img.setOrigin(img.getWidth() / 2, img.getHeight() / 2);
		img.setSize(img.getWidth() * 0.9f, img.getHeight() * 0.9f);
		this.setSize(img.getWidth() * 0.9f, img.getHeight() * 0.9f);
//		this.setOrigin(img.getWidth() / 2 - 50, img.getHeight() / 2 - 50);
		this.addActor(img);

		if (listener != null)
			setListener();
	}

	float i = 0.9f;

	private void setListener() {
		img.addListener(new ClickListener() {

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {

				// if (x >= 0 && x < img.getWidth() && y >= 0
				// && y < img.getHeight()) {
				// while (i < 1.5f)
				// i += 0.001;
				// img.setScale(i);
				img.setScale(1.4f);
				// }
				return super.touchDown(event, x, y, pointer, button);
			}

			@Override
			public void touchDragged(InputEvent event, float x, float y,
					int pointer) {

				// if (x >= 0 && x < img.getWidth() && y >= 0
				// && y < img.getHeight()) {
				// while(i < 1.5f)
				// i += 0.001;
				// img.setScale(i);
				img.setScale(1.4f);
				// } else {
				// while(i > 1)
				// i -= 0.001;
				// img.setScale(i);
				// }
				super.touchDragged(event, x, y, pointer);
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {

				if (x >= 0 && x < img.getWidth() && y >= 0
						&& y < img.getHeight()) {
					// while (i > 1)
					// i -= 0.001;
					img.setScale(i);
					if (ButtonLevel.this.listener != null) {
						ButtonLevel.this.listener.onUp(id);
					}
				}
				super.touchUp(event, x, y, pointer, button);
			}

		});
	}

}
